<?php
/**
 * 前台登陆和注册管理控制器
 */
class MemberController extends CommonController{
	private $model;
	public function __init(){
        parent::__init();
		$this->model = K('User');
	}
    /**
     * 注册
     */
    public function register(){
        if (IS_POST) {
            // 判断用户名是否已存在
            $username = Q('post.username');
            if (in_array($username,K('User')->getField('username',true))) $this->error('用户名已存在','',0.5);
            // 判断密码是否一致
            if (Q('post.pwd1')!=Q('post.pwd2')) $this->error('密码不一致','',0.5);
            //判断验证码是否错误
            if(strtoupper(Q('post.code'))!=$_SESSION['code']) $this->error('验证码错误','',0.5);
            // 验证通过后注册
            if (!$this->model->addReg()) $this->error($this->model->error);
            $this->success('注册成功',__ROOT__,0.5);


        }
        $this->display();
    }
    
    /**
     * 登录
     */
    public function login(){ 
    	if (IS_POST) {
    		// p(Q('post.'));　
            if (!$this->model->mlogin()) $this->error($this->model->error); 
			$this->success('登录成功',__ROOT__,0.5);
    	} 
    	$this->display();
    }
    /**
     * 退出
     */
    public function out(){
    	session(NULL);
    	$this->success('退出成功',__ROOT__,0.5);
    }
    /**
     * 显示验证码
     */
    public function code(){
    	$code = new Code;
    	$code->show(); 
    }
    /**
     * 异步检测验证码是否正确
     */
    public function ajaxCheckCode(){
    	// 如果不是ajax提交，则返回
    	if (!IS_AJAX) return;
    	$code = Q('post.code','','strtoupper');
    	// 如果验证码不正确
    	if ($code != session('code')) {
    		$msg = array('status'=>false,'message'=>'验证码不正确');
    	}else{
    		$msg = array('status'=>true,'message'=>'验证通过');
    	}
    	$this->ajax($msg);
    } 

    /**
     * 个人中心
     */
    public function member(){

        /*if (!isset($_SESSION['uid'])&&!isset($_SESSION['uname'])) {
            echo 1;
        }*/ 
        if (!session('uid')&&!session('uname')) {
            // 直接跳到前台登录页面
            go(U('Member/login'));
        }else{
            // 共用导航分类
            $this->category();
            
            $uid = session('uid');
            $this->assign('userInfo',M('user')->where("uid={$uid}")->find());
            $this->display();
        }
 
        
    } 
    /**
     * 修改资料
     */
    public function member_edit(){
        // 共用导航分类
        $this->category();
        $uid = Q('get.uid',0,'intval'); 
        if (IS_POST) {
            // p(Q('post.'));die; 
            if(!$this->model->editData($uid)) $this->error($this->model->error);
            $this->success('修改成功',U('Member/member'),0.5);
        }
        // 个人中心用户数据  
        $this->assign('userInfo',M('user')->where("uid={$uid}")->find());
        $this->display();
    }
    /**
     * uploadify上传
     */
    public function upload(){
        $upload = new Upload('Upload/Content/' . date('y_m'));
        $file = $upload->upload();
        if (empty($file)) {
            $this->ajax('上传失败');
        } else {
            $data = $file[0];
            $this->ajax($data);
        }
    }
    /**
     * 收货地址
     */
    public function member_address(){
        // 共用导航分类
        $this->category();
        $address_data = M('address')->all();
        $this->assign('a_data',$address_data);

        $this->display();
    }

    /**
     * 删除收货地址
     */
    public function del_address(){
        $adid = Q('get.adid',0,'intval');
        M('address')->where("adid='{$adid}'")->delete();
        $this->success('删除地址成功',U('member_address'),0.5);
    }
    /**
     * 评论
     */
    public function member_comment(){
        // 共用导航分类
        $this->category();
        /*$address_data = M('address')->all();
        $this->assign('a_data',$address_data);*/

        $this->display();
    }

    /**
     * 订单中心
     */
    public function member_order(){
        // 共用导航分类
        $this->category();
        /*$address_data = M('address')->all();
        $this->assign('a_data',$address_data);*/

        $this->display();
    }






    




















}
